package com.crazyice.lee.reader;

import com.crazyice.lee.data.Product;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class ProductList extends RichParallelSourceFunction<Product> {
    private boolean isRun=true;
    private DateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void run(SourceContext<Product> sourceContext) throws Exception {
        Random random=new Random();
        int number=1;
        while (isRun){
            Product product=new Product();
            product.setId(String.format("%010d",number++));
            product.setName("商品名称"+product.getId());
            product.setDescribe("商品配置时间："+ bf.format(new Date(System.currentTimeMillis())));
            product.setPrice(random.nextFloat());
            sourceContext.collect(product);
            if(number>100) break;
        }
    }

    @Override
    public void cancel() {
        isRun=false;
    }
}
